package com.usian.controller;

import com.usian.api.ProviderFeign;
import com.usian.pojo.Emp;
import com.usian.query.EmpDto;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("consumer")
public class EmpController {



    @Autowired
    private ProviderFeign providerFeign;

    /*@RequestMapping("findAll")
    @SentinelResource(value = "findAll",blockHandlerClass = GlobalBlockHandler.class,blockHandler = "findAllBlcok")
    public List<Emp> findAll(){

        //serverId:被调用方的服务名
        *//*ServiceInstance instance = client.choose("provider");
        String url ="http://"+instance.getHost()+":"+instance.getPort()+"/provider/findAll";
        List list = restTemplate.getForObject(url, List.class);*//*

        List<Emp> list = providerFeign.findAll();
        return list;
    }

    @RequestMapping("findById")
    @SentinelResource(value = "findByIds",
            fallback = "fallTest",
            fallbackClass = GlobalFailBack.class,
            blockHandler = "findByIdBolck",
            blockHandlerClass = GlobalBlockHandler.class)
    public Emp findByIds(int id){
        //int a =10/0;
        return providerFeign.findByIds(id);
    }

    *//*public Emp fallTest(int id){
        return new Emp(id,"failback内部错误");
    }*//*

    @RequestMapping("findById/{id}")
    public Emp findById(@PathVariable int id){
        //serverId:被调用方的服务名
        ServiceInstance instance = client.choose("provider");
        String url ="http://"+instance.getHost()+":"+instance.getPort()+"/provider/findById/"+id;
        Emp emp = restTemplate.getForObject(url, Emp.class);
        return emp;
    }

    @RequestMapping("findById/{id}/{name}")
    public Emp findById(@PathVariable int id,@PathVariable String name){
        //serverId:被调用方的服务名
        Emp emp = providerFeign.findByIdAndName(id, name);
        return emp;
    }

    */



    /**
     * 分页查询员工信息
     *
     * @param empDto
     * @return
     */
    @RequestMapping("/list")
    public PageResult<Emp> findByEmpDto(@RequestBody EmpDto empDto){
        int page =(empDto.getPage()-1)*empDto.getSize();
        empDto.setPage(page);
        return providerFeign.findByEmpDto(empDto);
    }

    /**
     * 保存员工  succ
     *
     * @param emp
     * @return
     */
    @PostMapping("/saveEmp")
    public String saveEmp(@RequestBody Emp emp){
        String result ="fail";
        if(emp.getEid()==null){
            result = providerFeign.saveEmp(emp);
        }else{
            result = providerFeign.updateEmp(emp);
        }
        return result;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Emp findById(@PathVariable Integer id){
        return providerFeign.findById(id);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public String deleteById(@RequestParam Integer id){
        providerFeign.deleteById(id);
        return "succ";
    }
}
